package com.yunchen.BooksManagementSystem.user.servlet;

import com.alibaba.fastjson.JSON;
import com.yunchen.BooksManagementSystem.manage.entity.borrowingRecord;
import com.yunchen.BooksManagementSystem.user.dao.userDaoImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "ReturnBookServlet", value = "/ReturnBookServlet")
public class ReturnBookServlet extends HttpServlet {
    private userDaoImpl ud=new userDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        BufferedReader br=request.getReader();
       // String params= br.readLine();
       // System.out.println(params);
        StringBuilder builder=new StringBuilder();
        String line=null;
        while ((line=br.readLine())!=null){
            builder.append(line);
        }
        System.out.println(builder);
        borrowingRecord brd= JSON.parseObject(String.valueOf(builder),borrowingRecord.class);
        String userName= brd.getName();
        String bookName= brd.getBookName();
        int count;
        try {
            count=ud.returnBook(userName,bookName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(count);
        response.setContentType("text/json;charset=UTF-8");
        //响应成功标识
        if(count>0){
            response.getWriter().write("申请归还成功");
        }else {
            response.getWriter().write("申请归还失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
